<?php

	require_once('class.db.php');	

	class User
	{

		private $userName;
		private $userPassword;
		private $userLoginDate;
		private $dbQuery;

		public function __construct($db)
		{
		 	//echo " Object Instantiated.";
		 	$this->dbQuery = $db;
		}
		
		
		public function setUserName($login_name)
		{
			$this->userName = $login_name;
		}
		
		public function setUserPassword($login_password)
		{
			$this->userPassword = md5($login_password);
		}
		
		public function userLogin()
		{
			//global $db;
			$userLoginResultBind = array(":userid" => $this->userName, ":userpwd" => $this->userPassword);
			$userLoginResult 	 = $this->dbQuery->select("users","User_ID=:userid AND User_Password=:userpwd",$userLoginResultBind,"User_ID,User_First_Name,User_Last_Name,User_Email_ID,Is_Active,Last_Login_Date");
			if(sizeof($userLoginResult) != 0)
			{
				$this->setUserLoginDate($userLoginResult[0]['User_ID']);
				$this->setUserSession($userLoginResult);
			}
		}
		
		private function LogUserLogin()
		{
			// can be used to send mail to me when user logs in.
		}
		
		private function getUserAccess($access_level)
		{
			$userAccessResult = $db->select("user_access_levels","Access_NO={$access_level}");
			return $userAccessResult; 
		}
		
		private function setUserLoginDate($user_name)
		{
			//global $db;	
			$this->userLoginDate	= date('Y-m-d H:i:s');
			$userLastLoginDate  	= array("Last_Login_Date" => $this->userLoginDate);
			$userLastLoginDateBind	= array(":userid" => $user_name);
			return $this->dbQuery->update("users",$userLastLoginDate,"User_ID = :userid",$userLastLoginDateBind);			
		}
		
		public function userRegistration()
		{
			
		}
		
		private function setUserSession($user_details)
		{
			session_regenerate_id();
			$userSessionId	= session_id();
			$_SESSION['user_details'] = $user_details;
		}
		
/*		public function setSession($sess_key="",$sess_value="")
		{
			if(! isset($sess_key) && ! isset($sess_value))
			{
				
			}
			$_SESSION["$sess_key"] = $sess_value;
 
		}*/
		
		public function userLogout()
		{
			unset($_SESSION);
			session_destroy();			
		}
	}

?>